package com.hs.core.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
 

public class PageQuery {
	
	private Map conditionMap;
	private PageParam pageParam;
	
	
	/**
	 * 
	 * @param pageParam
	 */
	public PageQuery(PageParam pageParam) {
		this.pageParam=pageParam;
		
		
	}
	
	/**
	 * 
 	 * 
	 * @param query
	 * @param pageParam
	 * @return
	 */
	public PageUtil getPageUtil(Query query,Session session) {
		// System.out.println("===:"+pageParam.getPerPageCount());
		PageUtil pu = new PageUtil();
		String sql = query.getQueryString();
		
		String countsql = getCountSql(sql);
		Query countquery = session.createQuery(countsql);
		this.conditionMap = pageParam.getConditionMap();
		HibernatePage hp = new HibernatePage(query, countquery,pageParam
				.getCurrentPageNo(), pageParam.getPerPageCount());
 		
		//System.out.println("hp.elements::::" + hp.elements);
		pu.setPageList(hp.elements);
		pu.setPageStr(this.getHtmlStr(hp, pageParam));
		pu.setAllnum(hp.totalElements);
		pu.setCurrentPage(hp.pageNumber); //设置当前页数
		pu.setNumberPerPage(hp.pageSize); //设置每页显示的记录数.

		return pu;

	}
	/**
	 * 
 	 * 
	 * @param query
	 * @param pageParam
	 * @return
	 */
	public PageUtil getPageUtil(Query query,Query countquery) {
		// System.out.println("===:"+pageParam.getPerPageCount());
		PageUtil pu = new PageUtil();
		String sql = query.getQueryString();
		
		String countsql = getCountSql(sql);
		//Query countquery = session.createQuery(countsql);
		this.conditionMap = pageParam.getConditionMap(); 
		HibernatePage hp = new HibernatePage(query, countquery,pageParam
				.getCurrentPageNo(), pageParam.getPerPageCount());
 		
		//System.out.println("hp.elements::::" + hp.elements);
		pu.setPageList(hp.elements);
		pu.setPageStr(this.getHtmlStr(hp, pageParam));
		pu.setAllnum(hp.totalElements);
		pu.setCurrentPage(hp.pageNumber); //设置当前页数
		pu.setNumberPerPage(hp.pageSize); //设置每页显示的记录数.

		return pu;

	}
	/**
	 * 
 	 * 
	 * @param query
	 * @param pageParam
	 * @return
	 */
	public PageUtil getPageUtil(Query query) {
		// System.out.println("===:"+pageParam.getPerPageCount());
		PageUtil pu = new PageUtil();
		String sql = query.getQueryString();
		
		String countsql = getCountSql(sql);
		//Query countquery = session.createQuery(countsql);
		this.conditionMap = pageParam.getConditionMap(); 
		HibernatePage hp = new HibernatePage(query,pageParam
				.getCurrentPageNo(), pageParam.getPerPageCount());
 		
		//System.out.println("hp.elements::::" + hp.elements);
		pu.setPageList(hp.elements);
		pu.setPageStr(this.getHtmlStr(hp, pageParam));
		pu.setAllnum(hp.totalElements);
		pu.setCurrentPage(hp.pageNumber); //设置当前页数
		pu.setNumberPerPage(hp.pageSize); //设置每页显示的记录数.

		return pu;

	}
	private static  String getCountSql(String allsql){
		StringBuffer countSql = new StringBuffer("select count(*) ");
		if(StringUtils.isNotBlank(allsql)){
			String sql = allsql.toLowerCase();
			int index = sql.indexOf("from");
			int orderindex = sql.lastIndexOf("order");
			System.out.println("index:" + index);
			System.out.println("orderindex:" + orderindex);
			if(index != -1 && -1 == orderindex){
				countSql.append(allsql.substring(index));
			}else if(index != -1 && -1 != orderindex){
				countSql.append(allsql.substring(index,orderindex));
			}
			
		}
		return countSql.toString();
	}
	
	
	/**
	 * 
	 * 功能:  
	 * 
	 * @param query
	 * @param pageParam
	 * @return
	 */
	public PageUtil getPageUtil(Criteria criteria) {
		PageUtil pu = new PageUtil();
		this.conditionMap = pageParam.getConditionMap();
		HibernatePage hp = new HibernatePage(criteria, pageParam
				.getCurrentPageNo(), pageParam.getPerPageCount());

		pu.setPageList(hp.elements);
		pu.setPageStr(this.getHtmlStr(hp, pageParam));
        pu.setAllnum(hp.totalElements);
		pu.setCurrentPage(hp.pageNumber); //设置当前页数
		pu.setNumberPerPage(hp.pageSize); //设置每页显示的记录数.
		return pu;

	}
	
	
	 
	/**
	 * 返回控制翻页的html当码
	 * 
	 * @return
	 */
	public String getHtmlStr(HibernatePage hp,PageParam pageParam) {
		// 当前页/总页数
		String pageIndicator = "| 第" + hp.pageNumber + "页 | 共"
				+ hp.getLastPageNumber() + "页 ";
		// 首页
		String firstPage = "| <a title=转到首页  onClick=\"setCurrentPageNO(1)\" style=cursor:hand>首页</a> ";
		// 处理是否显
		if (hp.pageNumber < 2) {
			firstPage = "| 首页 ";
		}

		// 尾页
		String lastPage = "| <a title=转到尾页  onClick=\"setCurrentPageNO("+hp.getLastPageNumber()+")\" style=cursor:hand>尾页</a> ";
		// 处理是否显示
		if (hp.pageNumber == hp.getLastPageNumber()) {
			lastPage = "| 尾页 ";
		}

		// previousPage
		String previousPage = "";
		int previousPageCount = 0;
		// 处理页数问题,往上翻最多翻到第一页。不可能为负数
		if (hp.pageNumber < 2) {
			previousPageCount = 1;
			previousPage = "| 上一页 ";
		} else {
			previousPageCount = hp.pageNumber - 1;
			previousPage = "| <a title=转到上一页   onClick=\"setCurrentPageNO("+previousPageCount+")\" style=cursor:hand>上一页</a> ";
		}

		// nextPage
		String nextPage = "";
		int nextPageCount = 0;
		if ((hp.pageNumber + 1) > hp.getLastPageNumber()) {
			nextPageCount = hp.getLastPageNumber();
			nextPage = "| 下一页 ";
		} else {
			nextPageCount = hp.pageNumber + 1;
			nextPage = "| <a title=转到下一页   onClick=\"setCurrentPageNO("+nextPageCount+")\" style=cursor:hand>下一页</a> ";
		}

	
		// Jump to
		String jumpTo = "<span>"
				+ "<input type=\"hidden\" name=\"searchSql\" value=\""
				+ pageParam.getSearchSql()
				+ "\" >"
				+ "| 每页 <input type=\"text\" class=\"com_textbox\" size=1 name=\"perPageCount\" value=\""
				+ (hp.pageSize)
				+ "\" onKeyPress=\"inputInt();if(event.keyCode==13) submitPageForm()\" maxLength=\"4\"" 
				+" errName='每页记录数' misType= \"string\" misNull=\"1\" errMsg='输入的记录数过大!'"
				+ ">条记录 | 转到"
				//+ "<img style=cursor:hand src=\""
				//+ STYLE_PATH
				//+ "images/go.gif\" align=\"absmiddle\" onclick=\"submitPageForm()\">"
				+ " <input type=\"text\" class=\"com_textbox\" name=\"currentPageNo\" size=1 value='" 
				+ hp.pageNumber
				+ "' onKeyPress=\"inputInt();if(event.keyCode==13) submitPageForm()\" maxLength=\"9\"" 
				+" errName='转到页数' misType= \"string\" misNull=\"1\" errMsg='输入的页数值过大!'"+
						">页 |</span>";

		return "<form id=\"pageForm\" method=\"POST\" action=\"" + pageParam.getBasePath()
				+ "\" name=\"pageForm\" >"
				+  "| 共有<font color=red>" + hp.totalElements
				+ "</font>条 " + pageIndicator + firstPage
				+ previousPage + nextPage + lastPage + jumpTo + "<br>"
				+ this.getConditonStr() + "</form>";
	}
	
	 
	/**
	 * 
	 * 功能: 获得页面上要显示的那些参数的显示。
 	 * 创建日期:2006-3-8
	 * @param conditionMap
	 * @return
	 */
	private String getConditonStr(){
		String conditionStr="";
		Set set=conditionMap.keySet();
		for (Iterator iter = set.iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			conditionStr+="<input type=\"hidden\" name=\""+name+"\" value=\""+conditionMap.get(name)+"\">";
		}
		return conditionStr;
		
	}
	
	
	
	
	
	
	
	
	

}
